#include "protreeitem.h"
#include "const.h"

ProTreeItem::ProTreeItem(QTreeWidget *view, const QString &name, const QString &path, int type)
    : QTreeWidgetItem (view, type), _path(path), _name(name), _root(this), _pre_item(nullptr), _next_item(nullptr) {}

ProTreeItem::ProTreeItem(QTreeWidgetItem *parent, const QString &name, const QString &path, QTreeWidgetItem *root, int type)
    : QTreeWidgetItem(parent, type), _path(path), _name(name), _root(root), _pre_item(nullptr), _next_item(nullptr) {}

const QString &ProTreeItem::GetPath()
{
    return _path;
}

QTreeWidgetItem *ProTreeItem::GetRoot()
{
    return _root;
}

void ProTreeItem::SetProItem(QTreeWidgetItem *item)
{
    _pre_item = item;
}

void ProTreeItem::SetNextItem(QTreeWidgetItem *item)
{
    _next_item = item;
}

ProTreeItem *ProTreeItem::GetPreItem()
{
    // return _pre_item; // error
    return dynamic_cast<ProTreeItem*>(_pre_item);
}

ProTreeItem *ProTreeItem::GetNextItem()
{
    // return _next_item; // error
    return dynamic_cast<ProTreeItem*>(_next_item);
}

ProTreeItem *ProTreeItem::GetLastPicChild()
{
    if(this->type() == TreeItemPic) return nullptr;

    auto child_count = this->childCount();
    if(child_count == 0) return nullptr;

    // 双向链表，但是可能会有文件夹的情况
    // 因为是找最后一个节点，所以从后向前找
    for(int i = child_count - 1; i >= 0; i--){
        auto* last_child = this->child(i);
        auto* last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        int item_type = last_tree_item->type();
        if(item_type == TreeItemPic){
            return last_tree_item;
        }

        // last_tree_item 是一个文件夹的话，就递归调用下去找
        last_child = last_tree_item->GetLastPicChild();
        if(last_child == nullptr){
            continue;
        }
        last_tree_item = dynamic_cast<ProTreeItem*>(last_child);
        return last_tree_item;
    }

    return nullptr;
}

ProTreeItem *ProTreeItem::GetFirstPicChild()
{
    if(this->type() == TreeItemPic) return nullptr;

    auto child_count = this->childCount();
    if(child_count == 0) return nullptr;

    // 从前向后遍历
    for(int i = 0; i < child_count - 1; i++){
        auto* first_child = this->child(i);
        auto* first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        int item_type = first_tree_item->type();
        if(item_type == TreeItemPic) return first_tree_item;

        first_child = first_tree_item->GetFirstPicChild();
        if(first_child == nullptr) continue;
        first_tree_item = dynamic_cast<ProTreeItem*>(first_child);
        return first_tree_item;
    }

    return nullptr;
}


